<?
require_once($r."_config/db.php");
require_once($r."_engine/c_sql_table.php");

$g_st_ets = new CSqlTable("emails_templates", "id");

class CEmailTemplate
{
	var $id, $data;
	
	function InitNew()
	{
		// Initialize
		$this->data["template_name"]	= "new_template";
		$this->data["email_subject"]	= "";
		$this->data["template_text"]	= "";
	}
	
	function Insert($values)
	{
		global $g_st_ets;
		return $g_st_ets->Insert($values);
	}

	function LoadById($id)
	{
		return $this->LoadByQuery(
			"SELECT * FROM emails_templates WHERE id='$id';"
		);
	}
	function LoadByQuery($query)
	{
		global $g_st_ets;
		$row = $g_st_ets->SelectRow($query);
		if (!$row) return false;
		$this->LoadFromArray($row);
		return true;
	}
	
	function LoadFromArray($row)
	{
		foreach($row as $f => $v)
		{
			if (is_int($f)) continue;
			
			if ($f == "id")
				$this->id = $v;
			else
				$this->data[$f] = $v;
		}
	}
	
	function Update($values)
	{
		$id = $this->id;
		global $g_st_ets;
		$this->LoadFromArray($values);
		return $g_st_ets->Update($values, $id);
	}
}

function GetEmailTemplates()
{
	global $g_st_ets;
	$rows = $g_st_ets->SelectRows("SELECT id, template_name, email_subject FROM emails_templates ORDER BY id ASC;");
	
	$results = array();
	
	foreach ($rows as $k => $v)
	{
		$n = new CEmailTemplate();
		$n->LoadFromArray($v);
		$results[] = $n;
	}
	
	return $results;
}

?>